﻿using System;
using System.ComponentModel;
using System.Runtime.Remoting;
using System.Security.Permissions;
using Present.Commands.Decorators;

namespace Present.Commands
{
    /// <summary>
    /// Dind't want to reference ServiceLocator
    /// </summary>
    public static class BackgroundWorkerFactory
    {
        static BackgroundWorkerFactory()
        {
            Create = () => new BackgroundWorkerDecorator();
        }

        public static Func<IBackgroundWorker> Create { get; set; }
    }

    /// <summary>
    /// Contract for BackgroundWorkers.
    /// Useful for testing and Classes that want to transparently swap between Synch and Asynch behaviour
    /// </summary>
    public interface IBackgroundWorker : IDisposable
    {
        [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure)]
        object GetLifetimeService();
        
        [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure)]
        object InitializeLifetimeService();
        
        [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure)]
        ObjRef CreateObjRef(Type requestedType);

        ISite Site { get; set; }
        IContainer Container { get; }
        bool CancellationPending { get; }
        bool IsBusy { get; }
        bool WorkerReportsProgress { get; set; }
        bool WorkerSupportsCancellation { get; set; }
        event EventHandler Disposed;
        void CancelAsync();
        void ReportProgress(int percentProgress);
        void ReportProgress(int percentProgress, object userState);
        void RunWorkerAsync();
        void RunWorkerAsync(object argument);
        event DoWorkEventHandler DoWork;
        event ProgressChangedEventHandler ProgressChanged;
        event RunWorkerCompletedEventHandler RunWorkerCompleted;
    }
}